package com.smit.livevideo.sample.dialog;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputFilter;
import android.text.InputType;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.smit.dvb.CIPinInfo;
import com.smit.livevideo.R;
import com.smit.livevideo.sample.util.SDKAPIUtil;
import com.smit.utils.LogUtil;

/**
 * Created by smfang on 2015/12/28.
 * CI MMI Pin Dialog
 */
public class CIMMIPinDialog extends BaseDialogFragment implements DialogInterface.OnKeyListener, Handler.Callback {
    private static final String TAG = CIMMIPinDialog.class.getSimpleName();
    private EditText ciPinEdit;
    private TextView ciPinText;
    private Button ciPinBtn;
    private Handler msgHandler;

    @Override
    public void onAttach(Activity activity) {
        LogUtil.trace(TAG, "onAttach~~~");
        super.onAttach(activity);
        this.activity = activity;
        msgHandler = new Handler(this);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
        clearPinText();
        setPinType();
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams windowParams = window.getAttributes();
        //设置灰背景为透明
        windowParams.dimAmount = 0.00f;
        window.setAttributes(windowParams);
        //设置无边框
        window.getDecorView().setBackgroundColor(getResources().getColor(R.color.transparent_background));
        window.setLayout(activity.getResources().getDimensionPixelSize(R.dimen.px710),
                activity.getResources().getDimensionPixelSize(R.dimen.px520));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        LogUtil.trace(TAG, "onCreateView~~~");
        View v = inflater.inflate(R.layout.dialog_ci_mmi_pin, container, false);
        ciPinEdit = (EditText) v.findViewById(R.id.ciPinEdit);
        ciPinText = (TextView) v.findViewById(R.id.enqTitleText);
        ciPinBtn = (Button) v.findViewById(R.id.pinOkBtn);
        ciPinEdit.requestFocus();

        ciPinBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String pinText = ciPinEdit.getText().toString();
                SDKAPIUtil.getCIControl().setPin(pinText);
            }
        });

        getDialog().setOnKeyListener(this);
        return v;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public boolean handleMessage(Message msg) {
        return false;
    }

    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        if (KeyEvent.KEYCODE_BACK == keyCode && event.getAction() == KeyEvent.ACTION_DOWN)
        {
            SDKAPIUtil.getCIControl().setPin("");
            return true;
        }
        return false;
    }

    private void setPinType()
    {
        if (SDKAPIUtil.getCIControl() == null)
        {
            LogUtil.trace(TAG, "CI_CONTROL is null!");
            return;
        }

        CIPinInfo ciPinInfo = SDKAPIUtil.getCIControl().getCIPinInfo();
        ciPinText.setText(ciPinInfo.title);

        if (1 == ciPinInfo.type)
        {
            ciPinEdit.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
        }
        else
        {
            ciPinEdit.setInputType(InputType.TYPE_CLASS_NUMBER);
        }

        if (0xff != ciPinInfo.length)
        {
            ciPinEdit.setFilters(new InputFilter[]
                    {new InputFilter.LengthFilter(ciPinInfo.length)});
        }
    }

    private void clearPinText() {
        LogUtil.trace(TAG, "clearPinText~~~");
        if (ciPinEdit == null)
        {
            LogUtil.trace(TAG, "clearPinText ciPinEdit is null");
            return;
        }
        ciPinEdit.clearComposingText();
        ciPinEdit.setText("");
    }
}
